package com.sudichina.sudichina.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Line;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.view.activity.login.LoginActivity;
import com.sudichina.sudichina.view.activity.main.BaseActivity;
import com.sudichina.sudichina.view.customview.xlistview.ItemAdapter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${杨伟乔} on 2017/3/7.
 * 邮箱: yangweiqiao@126.com
 * QQ: 463217558
 * 别人光鲜的背后或者有着太多不为人知的痛苦
 * 学习可以改变一切、 行动可以解决一切问题、
 */
public class LineManageActivity extends BaseActivity implements onRequestDataFinish, AdapterView.OnItemClickListener {
    @BindView(R.id.iv_back)
    ImageView privateIvBack;
    @BindView(R.id.xListView)
    ListView privateXListView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private ListView mListView;
    //	private ArrayAdapter<String> mAdapter;
    private ItemAdapter adapter;
    //	private Context context;

    private Handler mHandler;
    private int start = 0;
    private static int refreshCnt = 0;

    /**
     * Called when the activity is first created.
     */

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_line);
        ButterKnife.bind(this);
        tvTitle.setText("我的行程");

        //检测是否登录
        LoginActivity.startMeAndInitIsLogin(this, Contains.LINEMANAGER_EXTRA);


        init();

        mHandler = new Handler();
    }

    @Override
    protected void onResume() {
        geneItems();
        super.onResume();
    }

    private void init() {
        // TODO Auto-generated method stub
        mListView = (ListView) findViewById(R.id.xListView);

        adapter = new ItemAdapter(this);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(this);


    }


    private void geneItems() {
        HashMap<String, Object> hm = new HashMap<>();
/*
*
*  Long accountId; // 用户Id
 Long carId; // 车辆id
 Long dicId; // 车辆参数Id
 Long toZoneCode; // 到达地区code
 Long fromZoneCode; // 开始地区code
 String laLoPosition; // 经度，纬度
 String verifyAccountId;// 数据验证人员
 String type; // 发布类型
 String fromAddress; // 起点，详细地址
 Short infoValid; // 0 默认，3记录无效，2 手机号有效车牌无效，1手机号和车牌都有效

 Double priceDun; // 吨的单价
 Double priceSquare; // 方的单价
 Double priceKg; // 千克的单价
 Double priceAll; // 整车价格

 Double prickDoor; // 千克的单价
 Double prickStation; // 整车价格

 Date releaseTime; // 发车时间
 Date arriveTime; // 到达时间
 Short sendTime;// 短信发送次数
 Date sendDate;// 最近一次短信发送时间
 Date pushDate; // 最后一次推送日期
 Short pushTime; // 推送次数
 Short level; // 渠道等级 1分通公司，2大区级，3省级，4地市级
 Boolean isUse; // 是否发展了渠道商 0:没有 1有
 String addService; // 以逗号隔开的服务编码
 Boolean isLock; // 是否锁定 0:未锁定 1:已锁定
 Integer status;
 currentPage 当前页
 pageSize 每页大小
* */
        hm.put("accountId", MyApp.getCurrentUser().getId());

        NetUtils.requestData(Api.LISTLINES_BYTRANLINE_ASAPP, hm, this, new TypeToken<Result<List<Line>>>() {
        }.getType(), false);


    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
//        // TODO Auto-generated method stub
//        Intent intent = new Intent(getApplicationContext(), RechargeActivity.class);
////        intent.putExtra("item", items.get(position));
//        startActivity(intent);
//        overridePendingTransition(R.anim.slide_in_from_right, R.anim.remain_original_location);

    }


    @OnClick(R.id.iv_back)
    public void onClick() {


        finish();
    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {
        Result<List<Line>> bean1 = (Result) bean;
        int code = bean1.getCode();
        if (code == 1) {
//            Toastutils.showToast("查询成功");
            final List<Line> data = bean1.getData();
//            System.out.println("查询成功" + data.get(data.size()-1).toString());
            if (data != null && data.size() > 0) {
                adapter.relpaceData(data);
                adapter.notifyDataSetChanged();
            }

            adapter.setOnDeleteClickListener(new ItemAdapter.OnDeleteClickListener() {
                @Override
                public void onDeleteClick(final int position, Line line, final ItemAdapter itemAdapter) {

                    //  java.lang.String cannot be cast to com.sudichina.sudichina.bean.Result
                    Type type = new com.google.common.reflect.TypeToken<Result>() {
                    }.getType();
                    NetUtils.requestData(Api.REMOVETLINE + line.getId(), null, new onRequestDataFinish() {
                        @Override
                        public void requestdataFinish(String catalog, Object bean) {
                            try {
                                // TODO: 2017/3/24 0024
                                //接口文档39
                                //java.lang.ClassCastException: java.lang.String cannot be cast to com.sudichina.sudichina.bean.Result
                                Result bean1 = (Result) bean;
                                Toastutils.showToast(bean1.getCode() + "");
                                if (bean1.getCode() == 1) {
                                    Toastutils.showToast("服务器删除成功");
                                    data.remove(position);
                                    itemAdapter.notifyDataSetChanged();
                                }
                            } catch (Exception e) {


                            }


                        }

                        @Override
                        public void requestdataFailed(IOException e) {

                        }
                    }, type, false);

                }
            });

        }

    }

    @Override
    public void requestdataFailed(IOException e) {

    }
}
